package com.nit.lab.mapper.medical;

import com.nit.lab.dto.medical.BedDTO;
import com.nit.lab.entity.medical.BedEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Description
 * @Author HWY
 * @Date 2020/12/14 10:50
 **/
public interface BedMapper {

        /**
         * @Description: 获取单位表格数据
         * @Author HWY
         * @Date 2020/12/14 10:50
         */
        @Select(value = {"<script>SELECT " +
                "bed_id AS `key`, bed_id, sensor_id " +
                "FROM medical_bed " +
                "WHERE TRUE " +
                "<if test='sensorId != null'> AND sensor_id LIKE CONCAT('%', #{sensorId}, '%') </if>" +
                "ORDER BY "+"<if test='sortField != null'>" +
                "${sortField} ${sortOrder}, " +
                "</if>" +" bed_id ASC " +
                "LIMIT #{start}, #{length}" +
                "</script>"})
        List<BedDTO> list4Table(
                @Param("start") Integer start,
                @Param("length") Integer length,
                @Param("bedId") String bedId,
                @Param("sensorId") String sensorId,
                @Param("sortField") String sortField,
                @Param("sortOrder") String sortOrder) throws Exception;

        /**
         * @Description: 获取单位表格数据总条数
         * @Author HWY
         * @Date 2020/12/14 10:50
         */
        @Select(value = {"<script>SELECT COUNT(*) " +
                "FROM medical_bed " +
                "WHERE TRUE " +
                "<if test='sensorId != null'> AND sensor_id LIKE CONCAT('%', #{sensorId}, '%') </if>" +
                "</script>"})
        Long count4Table(@Param("sensorId") String sensorId) throws Exception;

        /**
         * @Description: 新增单位信息
         * @Author HWY
         * @Date 2020/12/14 10:50
         */
        @Insert("INSERT INTO medical_bed (" +
                "bed_id, bed_place, " +
                "sensor_id) " +
                "VALUES (#{bedEntity.bedId},#{bedEntity.bedPlace}, " +
                "#{bedEntity.sensorId})")
        @Options(useGeneratedKeys = true, keyProperty = "bedId", keyColumn = "bed_id")
        Integer add(@Param("bedEntity") BedEntity bedEntity) throws Exception;

        /**
         * @Description: 更新单位信息
         * @Author HWY
         * @Date 2020/12/14 10:50
         */
        @Update("UPDATE medical_bed " +
                "SET bed_place = #{bedEntity.bedPlace}, " +
                "sensor_id = #{bedEntity.sensorId} " +
                "WHERE bed_id = #{bedEntity.bedId}")
        Integer update(@Param("bedEntity") BedEntity bedEntity) throws Exception;

        /**
         * @Description: 删除单位信息
         * @Author HWY
         * @Date 2020/12/14 10:50
         */
        @Delete("DELETE FROM medical_bed WHERE bed_id = #{bedId}")
        Integer remove(@Param("bedId") Long bedId) throws Exception;

        @Select("SELECT bed_id AS `key`, bed_id, bed_name, bed_code " +
                ",bed_info" +
                "FROM tb_bed WHERE bed_code = #{bedCode} And bed_name = #{bedName}")
        BedDTO getBedById(@Param("bedId") Long bedId) throws Exception;

}
